﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： PaymentRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-10-30
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///订单支付信息 仓储层
	/// </summary>	
	public class PaymentRepository:BaseRepository<Payment>
	{		
    
        #region 构造

        public PaymentRepository() : base(){}

        public PaymentRepository(string connectionString) : base(connectionString){}

        public PaymentRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        public Payment GetPaymentByTradeNo(string trade_no,int businessType)
        {
            string sql = "SELECT * FROM Payment WITH(NOLOCK) WHERE BusinessType=@BusinessType AND PlatformCode=@PlatformCode";
            return FirstOrDefault(sql, new { BusinessType = businessType, PlatformCode = trade_no });
        }

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<PaymentDto> GetPages(int pageIndex, long pageSize, ReqPayment condition)
        {
            StringBuilder sql = new StringBuilder("select p.*,u.UserName,r.PayState,r.CardCode,c.Name as CardName from payment p with(nolock) inner join UserCardRela r with(nolock) on p.BusinessID = r.RelaId inner join UserInfo u with(nolock) on r.userid = u.userid inner join UserCard c with(nolock) on r.CardId = c.CardId");
            if (condition.PlatformId > 0)
            {
                sql.Append(" AND PayPlatform=@PayPlatformId");
            }
            if (condition.BusinessType > 0)
            {
                sql.Append(" AND BusinessType=@BusinessType");
            }
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND p.CreatedDate>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND p.CreatedDate<@EndDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.UserName))
            {
                sql.Append(" AND CHARINDEX(@UserName,u.UserName) >0");
            }


            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@PayPlatformId", condition.PlatformId, System.Data.DbType.Int32);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            dparams.Add("@UserName", condition.UserName, System.Data.DbType.String);
            dparams.Add("@BusinessType", condition.BusinessType, System.Data.DbType.Int32);
            return dbContext.Connection.Page<PaymentDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }
    }
}